package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.GsonUtils
import com.jt.assistant.R
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.bean.ScanDataBean
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_scan_home.*
import java.util.HashMap

/**
 * 退仓扫码查询  14714992  14715007  201023736627000001
 * 15182942 14715008 14987497
 */
class ReturnScanHomeActivity : BaseActivity<HomeViewModel>() {


    private var mCartonCode = ""
    private var mScanData: ScanDataBean? = null

    override fun onDestroy() {
        set_scan.unregisterEventBus()
        super.onDestroy()
    }

    override fun layoutId(): Int = R.layout.activity_return_scan_home

    override fun initView(savedInstanceState: Bundle?) {
        set_scan.initCamera(this, GlobalConfig.SCAN_TYPE_ONESHOT)
    }

    override fun initData() {
    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {
        set_scan.setOnScanListener {
            mCartonCode = it
            val map: MutableMap<String, String> = HashMap()
            map["cartonCode"] = it
            viewModel.getReturnsScanList(map)
        }

        bt_commit.setOnClickListener {
            startActivity(Intent(this@ReturnScanHomeActivity, ReturnScanActivity::class.java).apply {
                putExtra(GlobalConfig.SCAN_DATA, GsonUtils.toJson(mScanData))
                set_scan.clearData()
                tv_status.text = ""
                bt_commit.visibility = View.GONE
            })
        }


        viewModel.mGetReturnsScanList.observe(this, {
            if (it.isSuccess()) {
                val states = resources.getStringArray(R.array.waybill_state_name)
                mScanData = it.data

                tv_status.text = "共有${it.data.waybillList.size}个运单,  " +
                        "${it.data.totalCarton - it.data.scannedCarton}箱待扫\n" +
                        "运单状态:${states[it.data.state]}"
                bt_commit.visibility =
                    if (it.data.totalCarton - it.data.scannedCarton == 0) View.GONE else View.VISIBLE
            } else {
                bt_commit.visibility = View.GONE
            }
        })
    }

}